package com.henry.web;

import com.henry.service.SysTplCodeService;
import com.henry.web.vo.Response;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 峥桂 on 2017/5/20.
 */
@RestController
@RequestMapping("sys")
public class SysGeneratorController {

    @Autowired
    private SysTplCodeService sysTemplateService;

    @RequestMapping(value = "/showTables.show",method = RequestMethod.GET)
    public ModelAndView showTablesList(){
        ModelAndView view=new ModelAndView("sys/showTables");
        List<Map<String, Object>> list = sysTemplateService.findAllTables(new HashMap());
        view.addObject("showTablesList",list);
        return view;
    }

    @RequestMapping(value = "/showTablesSelect",method = RequestMethod.POST)
    public Response<List<Map<String, Object>>> showTablesSelect(){
        List<Map<String, Object>> list = sysTemplateService.findAllTables(new HashMap());
        Response<List<Map<String, Object>>> response=new Response<List<Map<String, Object>>>();
        response.setData(list);
        response.setSuccess();
        return response;
    }

    @RequestMapping(value = "/saveZip.do", method = RequestMethod.GET)
    public void saveZip(@RequestParam("tableNames")String[] tableNames , HttpServletResponse response) throws IOException {
//		String[] tableNames = new String[]{};
//		tableNames = JSON.parseArray(tables).toArray(tableNames);
        byte[] data = sysTemplateService.generatorCodeZip(tableNames);
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"tools_code.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }

//    /**
//     * 列表
//     */
//    @RequestMapping(value = "/tablesListPage.do", method = RequestMethod.POST)
//    public Response<List<Map<String, Object>>> tablesListPage(SysTemplateRequestBean sysTemplateRequestBean) {
//        Map<String, Object> map = new HashMap<>();
////		map.put("tableName", tableName);
//        map.put("offset", (sysTemplateRequestBean.getPage() - 1) * sysTemplateRequestBean.getiDisplayLength());
//        map.put("limit", sysTemplateRequestBean.getiDisplayLength());
//        Response<List<Map<String, Object>>> response=new Response<List<Map<String, Object>>>();
//        List<Map<String, Object>> list = sysTemplateService.findAllTables(map);
//        response.setCode(200);
//        response.setData(list);
//        return response;
//    }

}
